package com.cq.hd.admin.utils;

import org.apache.commons.lang3.CharUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串相关方法
 */
public class StringUtil {

    /**
     *  随机生成几位数
     * @param n
     * @return
     */
    public static String getRandStr(int n) {
        Random random = new Random();
        String sRand = "";
        for (int i = 0; i < n; i++) {
            String rand = String.valueOf(random.nextInt(10));
            sRand += rand;
        }
        return sRand;
    }


    /**
     * 将以逗号分隔的字符串转换成字符串数组
     *
     * @param valStr
     * @return String[]
     */
    public static String[] StrList(String valStr) {
        int i = 0;
        String TempStr = valStr;
        String[] returnStr = new String[valStr.length() + 1 - TempStr.replace(",", "").length()];
        valStr = valStr + ",";
        while (valStr.indexOf(',') > 0) {
            returnStr[i] = valStr.substring(0, valStr.indexOf(','));
            valStr = valStr.substring(valStr.indexOf(',') + 1, valStr.length());

            i++;
        }
        return returnStr;
    }

    public static String SHA1(String decript) {
        try {
            MessageDigest digest = MessageDigest
                    .getInstance("SHA-1");
            digest.update(decript.getBytes());
            byte messageDigest[] = digest.digest();
            // Create Hex String
            StringBuffer hexString = new StringBuffer();
            // 字节数组转换为 十六进制 数
            for (int i = 0; i < messageDigest.length; i++) {
                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexString.append(0);
                }
                hexString.append(shaHex);
            }
            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    // 获取6为随机数
    public static String randSix() {
        String[] baseStr = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0",};
        String str = "";
        for (int i = 0; i < 6; i++) {
            int index = ((int) (100 * Math.random())) % baseStr.length;
            str += baseStr[index];
        }
        return str;
    }

    public static boolean ckextensionid(String extensionid) {
        extensionid = extensionid.toLowerCase();
        Pattern pattern = Pattern.compile("[0-9a-z]{6}", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(extensionid);
        if (!matcher.matches()) {
            return false;
        }
        if (extensionid.contains("admin")) {
            return false;
        }
        pattern = Pattern.compile("[0-9]{6}", Pattern.CASE_INSENSITIVE);//纯数字不行，会员不行
        matcher = pattern.matcher(extensionid);
        boolean isNum = matcher.matches();
        if (isNum && extensionid.startsWith("100")) {
            return false;
        }


        int max = 4;
        // 单个字符不重复超过3次
        char[] charArray = extensionid.toCharArray();
        TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();
        for (int x = 0; x < charArray.length; x++) {
            if (!tm.containsKey(charArray[x])) {
                tm.put(charArray[x], 1);
            } else {
                int count = tm.get(charArray[x]) + 1;
                if (count > max) {
                    return false;
                }
                tm.put(charArray[x], count);
            }
        }


        //检查非连续字母
        int cnt = 1;
        int length = extensionid.length();
        for (int i = 0; i < length - 1; i++) {
            char tmp = CharUtils.toChar(extensionid.substring(i, i + 1));
            char next = CharUtils.toChar(extensionid.substring(i + 1, i + 2));
            if (tmp == next + 1) {
                cnt++;
            } else {
                cnt = 1;
            }
            if (cnt > max) {
                return false;
            }
        }

        cnt = 1;
        for (int i = 0; i < length - 1; i++) {
            char tmp = CharUtils.toChar(extensionid.substring(i, i + 1));
            char next = CharUtils.toChar(extensionid.substring(i + 1, i + 2));
            if (tmp == next - 1) {
                cnt++;
            } else {
                cnt = 1;
            }
            if (cnt > max) {
                return false;
            }
        }
        return true;
    }

    // 验证邀请码
    public static String ckext(String ext) {
        String retStr = "0";
        //5以上连续
        int cnt = 0;
        for (int i = 0; i < ext.length(); i++) {
            cnt = 0;
            String tmp = ext.substring(i, i + 1);
            for (int j = 0; j < ext.length(); j++) {
                if (tmp == ext.substring(j, j + 1)) {
                    cnt++;
                }
            }
            if (cnt >= 5) {
                return "1";
            }
        }
        char[] chars = ext.toCharArray();
        //检查非连续字母
        for (int i = 0; i < chars.length - 1; i++) {
            int tmp = chars[i];
            int next = chars[i + 1];
            if ((tmp == next + 1) || (tmp == next - 1)) {
                cnt++;
            }
            if (cnt >= 5) {
                return "2";
            }
        }
        //过滤1234
        if (ext.indexOf("1234") != -1) {
            return "3";
        }

        //过滤100开头
        if (ext.substring(0, 3).equals("100")) {
            return "4";
        }
        return retStr;

    }
}
